using Microsoft.Xna.Framework;
using RebelStar.Engine.Sprites;
using RebelStar.Engine.Graphics;
using Color = Microsoft.Xna.Framework.Graphics.Color;

namespace RebelStar.Engine.Maps
{
    public class MapEntry
    {
        public const int WIDTH = 128, HEIGHT = 64;
        static Color blendColor = Color.White;

        private TerrainSprite terrain;
        private StructureSprite structure;
        private int terrainState;
        private int structureState;

        public MapEntry(TerrainSprite terrain, int state)
        {
            this.terrain = terrain;
            this.terrainState = state;
        }

        internal void ChangeTerrain(TerrainSprite terr, int state)
        {
            this.terrain = terr;
            this.terrainState = state;
        }
        internal void AddStructure(StructureSprite struc, int state)
        {
            this.structure = struc;
            this.structureState = state;
        }
        public void Draw(ISurface surface, float x, float y, float scale)
        {
            if (terrain != null)
                DrawSprite(terrain, terrainState, surface, x, y, scale);

            if (structure != null)
                DrawSprite(structure, structureState, surface, x, y, scale);
        }
        public void DrawSprite(SpriteBase texture, int state, ISurface surface, float x, float y, float scale)
        {
            int dh = texture.Height - HEIGHT;
            int dw = texture.Width - WIDTH;
            surface.DrawSprite(
                texture,
                new Vector2(x - (dw > 0 ? dw*scale : -(WIDTH/2f - texture.Width/2f)*scale),
                            y - (dh > 0 ? dh*scale : -(HEIGHT/2f - texture.Height/2f)*scale)),
                texture.GetState(state),
                BlendColor,
                scale);
        }

        static public Color BlendColor
        {
            get { return blendColor; }
            set { blendColor = value; }
        }
        /// <summary>
        /// Average move points
        /// </summary>
        public int AvgMP
        {
            get
            {
                return terrain.MP + (structure == null ? 0 : structure.MP);
            }
        }
        public bool IsPassable
        {
            get
            {
                return terrain.IsPassable && (structure == null ? true : structure.IsPassable);
            }
        }
        public int Transparency
        {
            get
            {
                return (structure == null ? 100 : structure.Transparency);
            }
        }
    }
}